﻿using AutoMan.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Reflection;

namespace AutoMan.Helper
{
    public static class SqlsugarHelper
    {
        public static string rootPath = AppDomain.CurrentDomain.BaseDirectory;
        public static string dbPath = rootPath + "/winform.db";
        public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = $"Data Source={dbPath}",//连接符字串
            DbType = DbType.Sqlite,//数据库类型
            IsAutoCloseConnection = true, //不设成true要手动close
            MoreSettings = new ConnMoreSettings()
            {
                SqliteCodeFirstEnableDropColumn = true //只支持.net core
            },
            ConfigureExternalServices = new ConfigureExternalServices//设置可为空
            {
                EntityService = (c, p) =>
                {
                    // int?  decimal?这种 isnullable=true
                    if (c.PropertyType.IsGenericType &&c.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        p.IsNullable = true;
                    }
                    else if (c.PropertyType == typeof(string) &&c.GetCustomAttribute<RequiredAttribute>() == null)
                    { //string类型如果没有Required isnullable=true
                        p.IsNullable = true;
                    }
                }
            }
        });
    }
}
